package com.tepth.busfix.main.repair;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;

import com.tepth.busfix.R;
import com.tepth.busfix.adapter.FragmentAdapter;
import com.tepth.busfix.bean.reponse.BusInfo;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by zhangyu on 2017/9/6.
 */

public class RepairActivity2 extends AppCompatActivity {
    public static final String EXTRA_NAME = "busInfo";
    private Unbinder unbinder;
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.tab)
    TabLayout mTab;
    @BindView(R.id.viewPager)
    ViewPager mViewPager;
    BusInfo busInfo;
    private boolean isCanOperator;

    public static void startActivity(Activity activity, boolean show, BusInfo busInfo) {
        Intent intent = new Intent(activity, RepairActivity2.class);
        intent.putExtra("show", show);
        intent.putExtra(EXTRA_NAME, busInfo);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_repair2);
        unbinder = ButterKnife.bind(this);
        getIntentData();
        initView();
    }

    private void initView() {
        mToolbar.setTitle(getString(R.string.bus_number) + busInfo.getVehicleCode() + getString(R.string.repair_info));
        mTab.addTab(mTab.newTab().setText("维修作业"));
        mTab.addTab(mTab.newTab().setText("维保历史"));
        initToolbarNav(mToolbar);
        ContentFragment contentFragment = ContentFragment.newInstance(busInfo, isCanOperator);
        HistoryFragment historyFragment = HistoryFragment.newInstance(busInfo);
        mViewPager.setAdapter(new FragmentAdapter(getSupportFragmentManager(), contentFragment, historyFragment));
        mTab.setupWithViewPager(mViewPager);
    }

    private void getIntentData() {
        Intent intent = getIntent();
        busInfo = (BusInfo) intent.getSerializableExtra(EXTRA_NAME);
        isCanOperator = intent.getBooleanExtra("show", true);
    }

    protected void initToolbarNav(Toolbar toolbar) {
        toolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_24dp);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void onDestroy() {
        unbinder.unbind();
        super.onDestroy();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
        }
        return true;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }
}
